#!/bin/bash

WRITEQUOTA="/${QUOTA_TEST}/writeQuotaDir"
WRITEDIR="${FS_MOUNTPOINT}${WRITEQUOTA}"
LOGFILE="/tmp/quotalog"
global_used=0

msg0="测试所有用例";
msg1="测试打开文件时已 O_TRUNC 打开, 文件成功清 0";
msg2="测试 truncate 命令修改文件为 0";
msg3="测试 truncate 命令修改文件小于当前大小, 但是不为 0";
msg4="测试 truncate 命令修改文件大于当前大小, 但是不超出配额";
msg5="测试 truncate 命令修改文件大于当前大小, 且超出配额";
maxmsg=5

function print_help() {
    name="$0"
    echo "Usage:"
    for (( num=0; num<=${maxmsg}; num++ ));do
        eval echo "'    'sh ${name} ${num} '  ' \${msg${num}}"
    done
    exit
}

function init_all() {
    hard_limit=$(($1 * 1024 * 1024)) # 10MB
    clean_quota;
    mkdir -p ${WRITEDIR}; rm -f ${WRITEDIR}/*;
    orcafs-ctl --setquota --dir=${WRITEDIR} --hardlimit=${hard_limit} &> /dev/null
    sleep ${WAIT_CLIENT_SYNC};

    echo -n -e "=====清空目录并重新创建配额===== "
    quota_num=`orcafs-ctl --listquota | grep -w DIR | grep ${WRITEQUOTA} | wc -l`
    if ((${quota_num} == 1));then echo "pass"; else echo "not pass"; exit 1; fi; echo "";
}

function test_open_with_trunc() {
    test_num=1
    rm -rf ${WRITEDIR}/*
    bs_size=$(($1 *1024 *1024))
    
    echo "测试创建 $1MB 的文件, 然后已 O_TRUNC 打开覆盖写时文件会先被清空"
    cmd="dd if=/dev/zero of=${WRITEDIR}/file bs=${bs_size} count=1";echo -e "exec cmd:\n\t${cmd}"; eval ${cmd} &> ${LOGFILE}
    func_get_quota_used; old_used=${global_used}

    cmd="cat /dev/null > ${WRITEDIR}/file";echo -e "exec cmd:\n\t${cmd}"; eval ${cmd} &> ${LOGFILE}
    func_get_quota_used; new_used=${global_used}
    
    eval echo -n -e "==${test_num}==\${msg$test_num}==== "
    if ((${old_used} == ${bs_size})) && (( ${new_used} == 0)); then
        echo -e "success\n"; else echo -e "fail\n"; exit 1; fi;
}

function test_truncate() {
    rm -rf ${WRITEDIR}/*
    bs_size=$((3 *1024 *1024))
    truncate_size=$(($1 *1024 *1024))
    
    echo "测试创建 3MB 的文件, 然后使用 truncate 命令修改其大小为 $1 MB"
    cmd="dd if=/dev/zero of=${WRITEDIR}/file bs=${bs_size} count=1";echo -e "exec cmd:\n\t${cmd}"; eval ${cmd} &> ${LOGFILE}
    func_get_quota_used; old_used=${global_used}

    cmd="truncate -s ${truncate_size} ${WRITEDIR}/file";echo -e "exec cmd:\n\t${cmd}"; eval ${cmd} &> ${LOGFILE}
    func_get_quota_used; new_used=${global_used}
  
    eval echo -n -e "==${test_num}==\${msg$test_num}==== "
    if ((${old_used} == ${bs_size})) && (( ${new_used} == ${truncate_size})); then
        echo -e "success\n"; else echo -e "fail\n"; exit 1; fi;
}

function test_truncate_exceed() {
    rm -rf ${WRITEDIR}/*
    bs_size=$((3 *1024 *1024))
    truncate_size=$(($1 *1024 *1024))
    
    echo "测试创建 3MB 的文件, 然后使用 truncate 命令修改其大小为 $1 MB"
    cmd="dd if=/dev/zero of=${WRITEDIR}/file bs=${bs_size} count=1";echo -e "exec cmd:\n\t${cmd}"; eval ${cmd} &> ${LOGFILE}
    func_get_quota_used; old_used=${global_used}

    cmd="truncate -s ${truncate_size} ${WRITEDIR}/file";echo -e "exec cmd:\n\t${cmd}"; eval ${cmd} &> ${LOGFILE}
    exceed=`cat ${LOGFILE} | grep "failed to truncate" | wc -l`
    if (( ${exceed} == 1 )); then echo -e "\tFailed to truncate: Inappropriate ioctl for device"; fi
    
    func_get_quota_used; new_used=${global_used}
    eval echo -n -e "==${test_num}==\${msg$test_num}==== "
    if ((${old_used} == ${bs_size})) && (( ${new_used} == ${old_used})) && ((${exceed} == 1)); then
        echo -e "success\n"; else echo -e "fail\n"; exit 1; fi;
}

echo "/*******************************"
echo "      测试 truncate 文件         "
echo "*******************************/"
if [ "$1" == "--help" ] || [ -z "$1" ];then print_help; exit; fi

# init global quota env and check env
sh ${QUOTA_CHECK_ENV}
source ${QUOTA_COMMON_FUNC}

init_all 10; #MB
#set -x;
test_num=1; if (($1 == ${test_num})) || (($1 == 0));then test_open_with_trunc 1; fi #1 MB
test_num=2; if (($1 == ${test_num})) || (($1 == 0));then test_truncate 0; fi #2 MB
test_num=3; if (($1 == ${test_num})) || (($1 == 0));then test_truncate 1; fi
test_num=4; if (($1 == ${test_num})) || (($1 == 0));then test_truncate 6; fi
test_num=5; if (($1 == ${test_num})) || (($1 == 0));then test_truncate_exceed 12; fi

echo "/*******************************"
echo "    测试 truncate 文件全部成功   "
echo "*******************************/"
exit 0;